package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.core.common.gen.BaseServiceImpl;
import com.core.family.admin.domain.dto.RoleGp;
import com.core.family.admin.domain.po.SysRole;
import com.core.family.admin.domain.po.SysRoleG;
import com.core.family.admin.mapper.SysRoleGMapper;
import com.core.family.admin.mapper.SysRoleMapper;
import com.core.family.admin.service.SysRoleGService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-10 11:46:34
 * @Description 服务实现层
 */
@Service
public class SysRoleGServiceImpl extends BaseServiceImpl<SysRoleGMapper, SysRoleG> implements SysRoleGService {
    @Resource
    SysRoleMapper roleMapper;


    @Override
    public List<SysRoleG> listByGroupIds(Serializable orgId, Collection<?> groupIds) {
        LambdaQueryWrapper<SysRoleG> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(orgId), SysRoleG::getOrgId, orgId)
                .in(SysRoleG::getGid, groupIds);
        return this.baseMapper.selectList(wrapper);
    }


    @Override
    public List<SysRoleG> listByGroupId(Serializable orgId, Integer groupId) {
        LambdaQueryWrapper<SysRoleG> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(orgId), SysRoleG::getOrgId, orgId)
                .eq(SysRoleG::getGid, groupId);
        return this.baseMapper.selectList(wrapper);
    }

    /***
     * 添加组角色 如果角色不存在，则新增角色
     * @param roleGp
     * @return
     */
    @Override
    public boolean saveRoleGp(RoleGp roleGp) {
        SysRoleG roleG = new SysRoleG();
        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleGp, role);
        roleMapper.insert(role);
        roleGp.setRid(role.getId());
        BeanUtils.copyProperties(roleGp, roleG);

        this.baseMapper.insert(roleG);

        return true;
    }

}

